package gui;

import gui.helpers.WaitLayerUI;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import javax.swing.*;
import javax.swing.plaf.LayerUI;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ClientStartUpFrame extends JFrame {

	private GuiController gc;
	private BufferedImage image;
	private BufferedImage imageIcon;
	WaitLayerUI layerUI = null;

	public ClientStartUpFrame(GuiController guiController) {
		this.gc = guiController;
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				initGui();
				
			}
		});
	}
	

	protected void initGui() {
		setupFrame();
		setupServerSearch();
		
	}


	private void setupServerSearch() {
		
	}


	private void setupFrame() {
		imageIcon = null;
		image = null;
		try {
			image = ImageIO.read(this.getClass().getResource("audiobuddy.png"));
			imageIcon = ImageIO.read(this.getClass().getResource("audiobuddy1.png"));
		} catch (IOException e) {
			
		}
		this.setTitle("Searching server...");
		this.setVisible(true);
		//this.setDefaultCloseOperation();
		Dimension size = new Dimension(250, 300);
		this.setSize(size);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setIconImage(imageIcon);
		ImagePanel panel = createPanel(size);
		layerUI = new WaitLayerUI();
		JLayer<JPanel> jlayer = new JLayer<JPanel>(panel, layerUI);
		this.add(jlayer);
		pack();
		layerUI.start();
		
		
		
	}


	private ImagePanel createPanel(Dimension size) {
		ImagePanel background = new ImagePanel(image, size);
		return background;
		
		
	}


	public void stopSearching() {
		layerUI.stop();
		this.dispose();
		
	}

}


class ImagePanel extends JPanel {

	  private Image img;

	  public ImagePanel(BufferedImage image, Dimension size) {
		  this.img = image;
	    setPreferredSize(size);
	    setMinimumSize(size);
	    setMaximumSize(size);
	    setSize(size);
	    setLayout(null);
	  }

	  public void paintComponent(Graphics g) {
	    g.drawImage(img, 0, 0, 250, 300, null);
	  }

}


